package com.lastcode6175.bluetoothremotecontrol

import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.PointerInputChange
import androidx.compose.ui.input.pointer.PointerInputScope
import androidx.compose.ui.input.pointer.consumeAllChanges
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.input.pointer.positionChange
import androidx.compose.ui.unit.dp

@Composable
fun TouchpadView(
    controller: TouchpadController,
    modifier: Modifier = Modifier,
) {
    // 使用单一的 pointerInput 修饰符处理所有手势
    Box(
        modifier = modifier
            .fillMaxWidth()
            .height(200.dp)
            .clip(RoundedCornerShape(8.dp))
            .background(MaterialTheme.colorScheme.primaryContainer)
            .pointerInput(Unit) {
                detectTapGestures(onTap = { controller.onTap() })
            }
            .pointerInput(Unit) {
                // 自定义手势处理
                customGestureDetector(
                    onSingleFingerDrag = { change, dragAmount ->
                        // 单指拖动处理为光标移动
                        controller.onDrag(change, dragAmount)
                        change.consumeAllChanges()
                    },
                    onMultiFingerDrag = { panAmount ->
                        // 多指拖动处理为滚动
                        controller.onTransform(panAmount)
                    },
                    onDragEnd = {
                        controller.onDragEnd()
                    }
                )
            }
    )
}

/**
 * 自定义手势检测器，明确区分单指和多指操作
 */
suspend fun PointerInputScope.customGestureDetector(
    onSingleFingerDrag: (PointerInputChange, Offset) -> Unit,
    onMultiFingerDrag: (Offset) -> Unit,
    onDragEnd: () -> Unit
) {
    awaitPointerEventScope {
        var activePointerId: Long? = null
        
        while (true) {
            val event = awaitPointerEvent()
            val pointerCount = event.changes.size
            
            when (pointerCount) {
                0 -> {
                    // 所有手指抬起，触发拖动结束
                    activePointerId = null
                    onDragEnd()
                }
                1 -> {
                    // 单指操作 - 处理为光标移动
                    val change = event.changes[0]
                    
                    if (change.pressed) {
                        // 如果是新的触摸点或者没有活动的触摸点ID
                        if (activePointerId == null) {
                            activePointerId = change.id.value
                        }
                        
                        // 确保我们跟踪的是同一个触摸点
                        if (activePointerId == change.id.value) {
                            val dragAmount = change.positionChange()
                            if (dragAmount != Offset.Zero) {
                                onSingleFingerDrag(change, dragAmount)
                            }
                        }
                    } else {
                        // 手指抬起
                        if (activePointerId == change.id.value) {
                            activePointerId = null
                            onDragEnd()
                        }
                    }
                }
                else -> {
                    // 多指操作 - 处理为滚动
                    activePointerId = null
                    
                    // 计算所有触摸点的平均移动
                    val panAmount = event.changes
                        .filter { it.pressed }
                        .map { it.positionChange() }
                        .fold(Offset.Zero) { acc, offset -> acc + offset } / pointerCount.toFloat()
                    
                    if (panAmount != Offset.Zero) {
                        onMultiFingerDrag(panAmount)
                    }
                }
            }
        }
    }
}